package com.bdqn.rooms.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bdqn.rooms.beans.Euser;
import com.bdqn.rooms.service.EuserService;
import com.bdqn.rooms.util.JedisTemlateUtil;
import com.bdqn.rooms.util.ObjectTrans;
import com.bdqn.rooms.util.Result;
import com.bdqn.rooms.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jack
 * @since 2019-05-14
 */
@Controller
@RequestMapping("/euser")
public class EuserController {

    @Resource
    private EuserService euserServiceimpl;
    @Autowired
    private  JedisTemlateUtil jedisTemlateUtil;



    @RequestMapping("/demo")
    @ResponseBody
    public String demo(){

        return "name";
    }
    @RequestMapping("/demo1")
    public String demo1(){

        return "RuserPage";
    }
    @RequestMapping("/showPage")
    public String showPage(){
        return "RuserPage";
    }

    /**
     * 用户注册
     * @param euser
     * @return
     */
    @RequestMapping("/addEuser")
    @ResponseBody
    public boolean addEuser(Euser euser){
        Euser eusername = euserServiceimpl.selectOne(new EntityWrapper().eq("eusername", euser.getEusername()));
        if (eusername==null){
            euserServiceimpl.insert(euser);
            return true;
        }
        return false;
    }

    /**
     * Euser登录
     * @param euser
     * @return
     */
    @RequestMapping("/selectEuser")
    public String selectEuser(Euser euser, Model model){

        Euser eusername = euserServiceimpl.selectOne(new EntityWrapper().eq("eusername", euser.getEusername()));
       // JedisTemlateUtil jedisTemlateUtil = new JedisTemlateUtil();
        //把登录信息存入redis，并且用用户名来作为key值

        if (eusername!=null&&euser.getEpassword().equals(eusername.getEpassword())){
            System.out.println(eusername+eusername.getEusername());
            jedisTemlateUtil.DepositIn(eusername,eusername.getEusername());
            model.addAttribute("eusername",eusername.getEusername());
            return "forward:index";
        }
        return "forward:login-page";
    }

    @RequestMapping("/index")
    public String index(String eusername,Model model){
//
        String eusers = jedisTemlateUtil.takeOut(eusername);
        System.out.println(eusers);
        ObjectTrans objectTrans = new ObjectTrans();
        Euser euser = objectTrans.euserTans(eusers);
        System.out.println(euser);
        model.addAttribute("ename",euser.getEname());
        model.addAttribute("ephone",euser.getEphone());
        model.addAttribute("euserid",euser.getEuserID());

        return "index/index";
    }



    @RequestMapping("/menu1")
    public String menu1(){
        return "index/menu1";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "index/welcome";
    }

    @RequestMapping("/login-page")
    public String loginpage(){
        return "index/login-page";
    }
}

